package com.jincheng.web.controller.system;

import com.jincheng.common.constant.Constants;
import com.jincheng.common.core.domain.AjaxResult;
import com.jincheng.common.core.domain.entity.SysMenu;
import com.jincheng.common.core.domain.entity.SysUser;
import com.jincheng.common.core.domain.model.LoginBody;
import com.jincheng.common.core.domain.model.LoginUser;
import com.jincheng.common.core.domain.model.WxLoginBody;
import com.jincheng.common.utils.SecurityUtils;
import com.jincheng.framework.web.service.SysLoginService;
import com.jincheng.framework.web.service.SysPermissionService;
import com.jincheng.framework.web.service.TokenService;
import com.jincheng.system.service.ISysMenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author DairyJay
 */
@RequiredArgsConstructor
@RestController
public class SysLoginController {
    private final SysLoginService loginService;

    private final ISysMenuService menuService;

    private final SysPermissionService permissionService;

    private final TokenService tokenService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody, @RequestParam(required = false) Boolean isAdminPanel) {
        isAdminPanel = isAdminPanel != null && isAdminPanel;
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody, isAdminPanel);

        if (token == null) {
            return AjaxResult.error("您没有登录后台的权限，请访问前台");
        }
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 微信登录接口
     *
     * @param wxLoginBody 微信登录登录信息
     */
    @PostMapping("/wxLogin")
    public AjaxResult wxLogin(@RequestBody WxLoginBody wxLoginBody) {
        AjaxResult ajax = AjaxResult.success();

        // 生成令牌
        String token = loginService.wxLogin(wxLoginBody);
        ajax.put(Constants.TOKEN, token);

        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        if (!loginUser.getPermissions().equals(permissions)) {
            loginUser.setPermissions(permissions);
            tokenService.refreshToken(loginUser);
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
